<?php
/*
 * @Author: liuyang
 * @Description:  
 * @Date: 2019-07-15 17:17:30
 * @LastEditors: liuyang
 * @LastEditTime: 2019-07-30 00:25:28
 * @Version: 1.0
 */

namespace app\admin\controller;

use think\Db;
use think\Controller;
use app\common\model\SQL;
use think\exception\PDOException;
use app\common\model\UserModel;
use app\common\model\RoleModel;
use app\admin\validate\UserValidate;
use think\facade\Session;

/**
 * 用户管理
 */
class UserController extends Controller
{
    
    public function index()
    {
        return view();
    }

    public function page()
    {
        if (request()->isAjax()) {
            $post = request()->post();
            $user = new UserModel();
            $SQL = new SQL();
            $SQL->whereIf(!empty($post['id']), 'id', $post['id'])
                ->whereIf(!empty($post['sex']), 'sex', $post['sex'])
                ->whereIf(!empty($post['username']), 'username', $post['username']);
            $order = '';
            if (!empty($post['filed']) && !empty($post['order'])) {
                $order = $post['filed'] . ' ' . $post['order'];
            }
            $result = $user->page(($post['page'] - 1) * $post['limit'], $post['limit'], $SQL->get(), $order);
            return pagejson($result['data'], $result['count']);
        }
    }

    public function add()
    {
        if (!request()->isAjax()) {
            $data['user'] = null;
            $data['rolelist'] = RoleModel::all();
            return view('form', $data);
        }
        //验证
        $vlidate = new UserValidate();
        $result = $vlidate->check(request()->post());
        if(!$result){
            return zbcjson(false,$vlidate->getError());
        }

        $user = new UserModel();
        $user->allowField(true)->save(request()->post());
        //添加角色
        if (request()->has('roleIds', 'post')) {
            $user->roles()->saveAll(request()->post('roleIds'));
        }
        return zbcjson();
    }

    public function edit($id = 0)
    {
        if (!request()->isAjax()) {
            $data['user'] = UserModel::with('roles')->findOrEmpty($id);
            $data['rolelist'] = RoleModel::all();
            return view('form', $data);
        }
        //验证
        $vlidate = new UserValidate();
        $result = $vlidate->check(request()->post());
        if(!$result){
            return zbcjson(false,$vlidate->getError());
        }
        //数据处理
        $user = UserModel::find(request()->post('id'));
        $user->allowField(true)->save(request()->post(), ['id' => request()->post('id')]);
        $user->roles()->detach();
        if (request()->has('roleIds', 'post')) {
            $user->roles()->saveAll(request()->post('roleIds'));
        }

        return zbcjson();
    }

    public function delete($id = null)
    {
        $result  = false;
        //批量删除不是事务的
        if (request()->has('list', 'post')) {
            $result = UserModel::destroy(request()->post('list'));
        }
        if (!empty($id)) {
            $result = UserModel::destroy($id);
        }
        if ($result) {
            return zbcjson(true);
        }
        return zbcjson(false, '删除错误');
    }
}
